package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.service.SystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.info.GitProperties;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;

import javax.annotation.PostConstruct;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import static io.renren.common.utils.SystemInfoUtils.computMem;
import static io.renren.common.utils.SystemInfoUtils.getPercent;

/**
 * @author fun.pengzh
 * @class io.renren.modules.sys.service.impl.SystemInfoServiceImpl
 * @desc
 * @since 2022-01-23
 */
@Service
public class SystemInfoServiceImpl implements SystemInfoService {

    @Autowired
    private GitProperties gitProp;

    @Value("${renren.version}")
    private String codeVersion;

    @Value("${renren.build.time}")
    private String buildTime;

    private SystemInfo systemInfo;

    private Map<String, String> sysEnv;

    private Properties sysPop;

    private OperatingSystemMXBean operatingSystemMXBean;

    @PostConstruct
    public void init() {
        //oshi 获取当前系统信息
        systemInfo = new SystemInfo();
        //获取当前的系统环境变量
        sysEnv = System.getenv();
        //获取当前的系统属性集
        sysPop = System.getProperties();
        //获取当前的系统 MXBean
        operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    }

    @Override
    public Map<String, Object> getSystemInfo() {
        Map<String, Object> info = new HashMap<>();

        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        CentralProcessor processor = hardware.getProcessor();
        GlobalMemory memory = hardware.getMemory();

        //系统信息
        info.put("name", "renren-fast");
        info.put("codeVer", gitProp.getBranch() + " | " + codeVersion + " | " + buildTime);
        info.put("os", sysEnv.get("OS"));
        info.put("sysVer", sysPop.getProperty("os.name"));
        info.put("arch", sysPop.getProperty("os.arch"));
        info.put("cpus", processor.getPhysicalProcessorCount() + "核" + processor.getLogicalProcessorCount() + "线程");
        info.put("totalMem", computMem(memory.getTotal()));
        info.put("freeMem", computMem(memory.getAvailable()));
        info.put("percentMem", getPercent(memory.getAvailable(), memory.getTotal()));
        info.put("sysLang", Locale.getDefault());
        info.put("sysRate", operatingSystemMXBean.getSystemLoadAverage());
        info.put("workSpace", sysPop.getProperty("user.dir"));
        info.put("timeArea", ZoneId.systemDefault());

        //JVM信息
        info.put("jvmInfo", sysPop.getProperty("java.vm.name"));
        info.put("jvmVer", sysPop.getProperty("java.version"));
        info.put("javaHome", sysPop.getProperty("java.home"));
        info.put("jvmFree", computMem(Runtime.getRuntime().freeMemory()));
        info.put("jvmTotal", computMem(Runtime.getRuntime().totalMemory()));
        info.put("jvmMax", computMem(Runtime.getRuntime().maxMemory()));
        info.put("who", System.getProperty("user.name"));

        return info;
    }

}
